package com.mousycoder.redis.controller;
import java.util.Date;
import java.util.Random;

import com.mousycoder.redis.RedisService;
import com.mousycoder.redis.domain.RedPacketInfo;
import com.mousycoder.redis.domain.RedPacketRecord;
import com.mousycoder.redis.mapper.RedPacketInfoMapper;
import com.mousycoder.redis.mapper.RedPacketRecordMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * TODO
 *
 * @author mousycoder
 * @version 1.0
 * @date 2022/4/25 11:21 AM
 */
@RestController
public class RedPacketController {

    @Resource
    private RedisService redisService;

    @Resource
    private RedPacketInfoMapper redPacketInfoMapper;

    @Resource
    private RedPacketRecordMapper redPacketRecordMapper;

    public static final String TOTAL_NUM = "_totalNum";

    public static final String TOTAL_AMOUNT = "_totalAmount";


    @ResponseBody
    @RequestMapping("/addPacket")
    public String saveRedPacket(Integer uid,Integer totalNum,Integer totalAmount){
        RedPacketInfo redPacketInfo = new RedPacketInfo();
        redPacketInfo.setId(uid);
        redPacketInfo.setRedPacketId(System.currentTimeMillis());
        redPacketInfo.setTotalAmount(totalAmount);
        redPacketInfo.setTotalPacket(totalNum);
        redPacketInfo.setRemainingAmount(totalAmount);
        redPacketInfo.setRemainingPacket(totalNum);
        redPacketInfo.setUid(uid);
        redPacketInfo.setCreateTime(new Date());
        redPacketInfo.setUpdateTime(new Date());
        redPacketInfoMapper.insert(redPacketInfo);
        redisService.set(redPacketInfo.getRedPacketId()+"_totalNum",totalNum+"");
        redisService.set(redPacketInfo.getRedPacketId()+"_totalAmount",totalAmount+"");
        return "success";

    }


    /**
     * 抢红包
     *
     * @param redPacketId
     * @return
     */
    @ResponseBody
    @RequestMapping("/getPacket")
    public Integer getRedPacket(long redPacketId){
        String redPacketName = redPacketId + TOTAL_NUM;
        String num = (String)redisService.get(redPacketName);
        if (StringUtils.isNotBlank(num)){
            return Integer.parseInt(num);
        }
        return 0;
    }

    /**
     * 拆红包
     *
     * @param redPacketId
     * @return
     */
    @ResponseBody
    @RequestMapping("/getRedPacketMoney")
    public String getRedPacketMoney(int uid ,long redPacketId){
        Integer randomAmount = 0;
        String redPacketName = redPacketId + TOTAL_NUM;
        String totalAmountName = redPacketId + TOTAL_AMOUNT;
        String num = (String) redisService.get(redPacketName);
        if (StringUtils.isBlank(num) || Integer.parseInt(num) == 0) {
            return "抱歉 ！红包已经抢完了";
        }

        String totalAmount = (String) redisService.get(totalAmountName);
        if (StringUtils.isNotBlank(totalAmount)){
            Integer totalAmountInt = Integer.parseInt(totalAmount);
            Integer totalNumInt = Integer.parseInt(num);
            Integer maxMoney  = totalAmountInt / totalNumInt * 2;
            Random random = new Random();
            randomAmount = random.nextInt(maxMoney);
        }

        redisService.decr(redPacketName,1);
        redisService.decr(totalAmountName,randomAmount);
        updateRacketInDB(uid, redPacketId,randomAmount);
        return randomAmount + "";
    }

    public void updateRacketInDB(int uid, long redPacketId, int amount) {
        RedPacketRecord redPacketRecord = new RedPacketRecord();
        redPacketRecord.setUid(uid);
        redPacketRecord.setRedPacketId(redPacketId);
        redPacketRecord.setAmount(amount);
        redPacketRecord.setCreateTime(new Date());
        redPacketRecordMapper.insertSelective(redPacketRecord);
    }

}
